<?php defined('SYSPATH') or die('No direct script access.');
/**
 * @author thanh.dang
 * @todo Category Controller
 */
class Controller_Admin_Category extends Controller_Admin_Layout
{
    function __construct(Request $request, Response $response) {
        parent::__construct($request, $response);
        
    }
    
    function action_index($path = null)
    {
    	
    	if(Request::current()->method() === 'POST')
    	{
    		$formData = Arr::extract($_POST,array('parent_id','id','name','description','active'));
    		
    		$valid = Validation::factory($formData)
    									->rule('name','not_empty');
    		if ($valid->check())
    		{
	    		if(is_null($formData['id'])){
	    			$result = $this->create($formData);	
	    		}else{
	    			$result = $this->update($formData);
	    		}
	    		
	    		if($result instanceof Category){
	    			//create new category successfully
	    			
	    		}else if($result instanceof Error){
	    			//an error occured
	    		}
	    			
    		}else {
    			FlashMessage::getInstance()->add($valid->errors('category','true'));
    		}
    	}    	
    	
    	if(is_null($path)){
    		$category = Libs_Category::getInstance()->getRoot();
    	}else{
    		/**
    		 * parse category path
    		 * each category separate by slash (/)
    		 * example root/cat/subcat 
    		 */
    		//$this->auto_render = false;
    		$paths = explode('/',$path);
    		$current_category_name = array_pop($paths);
    		
    		$category = Libs_Category::getInstance()->getCategoryBySlug($current_category_name);
    	}
    	
        $this->set_content(View::admin('category/index')->set(array(
            'list' => View::admin('category/list')->set(array(
        					'category' => $category,
        					'path' => $path
        				)),
        	'breadcrumb' => View::admin('category/breadcrumb')->set(array(
							'paths' => isset($paths) ? $paths : $path,
        					'current_category_name' => isset($current_category_name) ? $current_category_name : ''       					
        				)),
        	'category' => $category,
        	'form' => View::admin('category/form')->set(array(
        		'parent' => $category,
        		'category' => new Category()
        	))
        )));
    }

    public function action_edit($slug = null)
    {
    	if(is_null($slug)){
    		$this->response->status(404);
    	}
    	
    	$category = Libs_Category::getInstance()->getCategoryBySlug($slug);
    	if(!$category){
    		$this->response->status(404);
    	}else{
    		$this->set_content(View::admin('category/edit')->set(array(
	    		'form' => View::admin('category/form')->set(array(
	    			'parent' => $category->getNode()->getParent(),
	    			'category' => $category,
    				'path' 	=> new Category_Path($category),
    				//'selectbox' => Libs_Category::getInstance()->render('root','category/render/selectbox','parent_id',$category->getNode()->getParent()->id,$category->id)
	    		)),
	    	)));	
    	}
    }
    
    public function action_delete($slug = null)
    {
    	
    	if(is_null($slug)){
    		$this->response->status(404);
    	}else{
    		$category = Libs_Category::getInstance()->getCategoryBySlug($slug);
    		$path = '';
    		if($category){
    			$path = new Category_Path($category);
    			$path = $path->getPath();
    			$category->getNode()->delete();
    		}
    		
    		$this->request->redirect(Route::url('category-tree',array('path' => $path)));
    	}
    	
    }
    
    private function create(array $formData)
    {
    	return Libs_Category::getInstance()->saveCategory($formData);
    }
    
    private function update(array $formData)
    {
    	return Libs_Category::getInstance()->saveCategory($formData,false);
    }
    
    function action_test()
    {
        $this->auto_render = false;
        //Doctrine_Record::STATE_CLEAN
        $view = Libs_Category::getInstance()->render('root');
        echo $view->render();
        
        //var_dump($cat->getNode()->getDescendants(null,true)->toArray());
        //var_dump(get_class_methods($cat->getNode()));
        
        //$tree = CategoryTable::getInstance()->getTree()->fetchTree();
        //var_dump($tree->toArray());
    }
}